package com.rsk.api;



import javax.ws.rs.QueryParam;

import com.rsk.utils.result.ResultBuilder;
import com.rsk.utils.result.ResultCode;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.web.bind.annotation.*;

import com.alibaba.dubbo.config.annotation.Reference;
import com.rsk.api.common.ReturnUtils;
import com.rsk.service.SysRoleService;
import com.rsk.utils.result.Result;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value="/sys/role")
public class SystemRoleApi {
	
	@Reference(interfaceClass = SysRoleService.class, protocol = "dubbo")
	private SysRoleService sysRoleService;
	
	
	//@RequiresPermissions("sys_users:save")
	@PostMapping(value="/saveSysRole" )
	public Result<String> saveSysRole(@QueryParam("roleName") String roleName,
		   							  @QueryParam("roleCode") String roleCode,
		   							  @QueryParam("menuId") String  menuId,
		   							  @QueryParam("authId") String  authId,
									  @RequestHeader("Authorization") String Authorization){
		//数据校验
		//String operatorUser = JwtUtil.getClaim(Authorization, "account");
		Long operatorUser = 123L;
		Long menuIds[] = (Long[])ConvertUtils.convert(menuId,Long.class);
		Long authIds[] = (Long[])ConvertUtils.convert(authId,Long.class);
		menuIds = new Long[]{1L,2L,3L};
		authIds = new Long[]{4L,5L,6L};
		int i = sysRoleService.saveSysRole(roleName, roleCode, menuIds, authIds, operatorUser);
		return ReturnUtils.returnDataByOne(i);
	}





	//@RequiresPermissions("sys_users:update")
	@PostMapping(value="/updateSysRole" )
	public Result<String> updateSysRole(@QueryParam("id") Long id,
									    @QueryParam("roleName") String roleName,
									    @QueryParam("roleCode") String roleCode,
									    @RequestHeader("Authorization") String Authorization){
		//数据校验
		//String operatorUser = JwtUtil.getClaim(Authorization, "account");roleName
		Long operatorUser = 123L;
		int i = sysRoleService.updateSysRole(id, roleName, roleCode, operatorUser);
		return ReturnUtils.returnDataByOne(i);
	}
	

	//@RequiresPermissions("sys_users:del")
	@PostMapping(value="/delSysRole" )
	public Result<String> delSysRole(@QueryParam("id") Long id,
									 @RequestHeader("Authorization") String Authorization){
		//String operatorUser = JwtUtil.getClaim(Authorization, "account");
		Long operatorUser = 123L;
		int i = sysRoleService.delSysRole(id, operatorUser);
		return ReturnUtils.returnDataByOne(i);
	}



	//查询角色
	//@RequiresPermissions("sys_users:del")
	//@PostMapping(value = "/selectSysRole")
	@SuppressWarnings("rawtypes")
	@PostMapping(value="/selectSysRole" )
	public Result<List<Map>> selectSysRole(@QueryParam("menuName") String roleName,
										 @QueryParam("menuUrl") String roleCode,
										 @RequestHeader("Authorization") String Authorization){
		//String operatorUser = JwtUtil.getClaim(Authorization, "account");
		Long operatorUser = 123L;
		List<Map> list = sysRoleService.selectSysRole(roleName, roleCode);
		return ResultBuilder.success(list,ResultCode.SUCCESS);
	}

	//获取菜单及权限组合数据
	/**
	 * 角色授权
	 *
	 * 用角色id去查询他对应的权限id  在用这个权限id去查询他能展示出来的菜单   然后去查菜单id之类的东西 把这个菜单给前端
	 *
	 * 角色权限中间表插入数据，角色id 跟权限id 对应【id主键自增，可能会变】，或者角色编码跟权限编码对应，
	 * 可以一条数据对应一个权限也可以，可以一条数据对应多个权限,逗号分开
	 */

}
